package com.guigu.controller;

import com.guigu.pojo.SysUsers;
import com.guigu.service.SysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台员工表 前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/sys-users")
public class SysUsersController {
    @Autowired
    private SysUsersService sysUsersService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> params) {
        String loginName = params.get("loginName");
        String password = params.get("password");
        SysUsers user = sysUsersService.login(loginName, password);
        Map<String, Object> result = new HashMap<>();
        if (user != null) {
            result.put("success", true);
            result.put("message", "登录成功");
            result.put("user", user);
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    @PostMapping("/user-list")
    public Map<String, Object> getUserList(@RequestBody SysUsers queryParams) {
        List<SysUsers> userList = sysUsersService.getUserList(queryParams);
        Map<String, Object> result = new HashMap<>();
        if (userList != null) {
            result.put("success", true);
            result.put("message", "获取用户列表成功");
            result.put("userList", userList);
        } else {
            result.put("success", false);
            result.put("message", "获取用户列表失败");
        }
        return result;
    }

    @PostMapping("/add-user")
    public Map<String, Object> addUser(@RequestBody SysUsers sysUsers) {
        Map<String, Object> result = new HashMap<>();
        int rows = sysUsersService.addUser(sysUsers);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "新增用户成功");
        } else {
            result.put("success", false);
            result.put("message", "新增用户失败");
        }
        return result;
    }

    @PostMapping("/update-user")
    public Map<String, Object> updateUser(@RequestBody SysUsers sysUsers) {
        Map<String, Object> result = new HashMap<>();
        int rows = sysUsersService.updateUser(sysUsers);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "修改用户成功");
        } else {
            result.put("success", false);
            result.put("message", "修改用户失败");
        }
        return result;
    }

    @PostMapping("/delete-user")
    public Map<String, Object> deleteUser(@RequestBody Map<String, Integer> params) {
        Integer userId = params.get("userId");
        Map<String, Object> result = new HashMap<>();
        int rows = sysUsersService.deleteUserById(userId);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "删除用户成功");
        } else {
            result.put("success", false);
            result.put("message", "删除用户失败");
        }
        return result;
    }

    @PostMapping("/delete-users")
    public Map<String, Object> deleteUsers(@RequestBody Map<String, List<Integer>> params) {
        List<Integer> userIds = params.get("userIds");
        Map<String, Object> result = new HashMap<>();
        int rows = sysUsersService.deleteUsersByIds(userIds);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "批量删除用户成功");
        } else {
            result.put("success", false);
            result.put("message", "批量删除用户失败");
        }
        return result;
    }
}
